package com.link.play.fit.ai.ui.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.HomeMenuDialogLayoutBinding

class HomeMenuDialog(context: Activity, val onclick:(Int) -> Unit) {
    var mContext: Activity = context
    var dialog: Dialog?=null
    lateinit var itemBinding: HomeMenuDialogLayoutBinding
    @SuppressLint("ClickableViewAccessibility", "InflateParams")
    fun builder(): Dialog {

        val view: View = LayoutInflater.from(mContext).inflate(R.layout.home_menu_dialog_layout, null)
         itemBinding = HomeMenuDialogLayoutBinding.bind(view)
        dialog = Dialog(mContext)
        dialog?.setContentView(view)
        val window: Window? = dialog?.window
        window?.run {
            setBackgroundDrawable(ColorDrawable())
            setDimAmount(0.4f)
            setGravity(Gravity.BOTTOM)
            val layoutParams = attributes
            //设置dialog可以穿透并且有蒙层
            layoutParams.flags =
                    //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_DIM_BEHIND
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
//            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            // layoutParams.verticalMargin = 0.06f
            attributes = layoutParams
            decorView.setPadding(0, 0, 0, 0)
        }
        itemBinding.contentLayout.setOnTouchListener { _, event ->

            val height: Int = itemBinding.layout1.top
            val bottom: Int = itemBinding.layout2.bottom

            val y = event.y
            if (event.action == MotionEvent.ACTION_UP) {
                if (y < height || y>bottom) {
                    dialog?.dismiss()
                }
            }
            true
        }
        itemBinding.logExerciseLayout.setOnClickListener {
            onclick.invoke(1)
            dialog?.dismiss()
        }
        itemBinding.savedFoodsLayout.setOnClickListener {
            onclick.invoke(2)
            dialog?.dismiss()
        }
        itemBinding.foodDatabaseLayout.setOnClickListener {
            onclick.invoke(3)
            dialog?.dismiss()
        }
        itemBinding.scanFoodLayout.setOnClickListener {
            onclick.invoke(4)
            dialog?.dismiss()
        }
        return dialog!!
    }


}